/**
 * 数据清洗，用于把 tplData.form.data_range tplData.form.form_data.fieldSources 反向覆盖到 tplData.form.form_data
 * 解决radio select 超链接 checkbox条件存储的是显示值，但字段存储的是实际值，无法直接计算的bug
 */
import cloneDeep from 'lodash/cloneDeep'
import isArray from 'lodash/isArray'
import isNumber from 'lodash/isNumber'
import get from 'lodash/get'
import set from 'lodash/set'
import { isObject } from '../framework/common/utils'

export function mergecalculateFormData(tplData){
  let data_range = tplData.form.data_range;
  let field_sources = tplData.form.form_data.fieldSources;
  let ff_data = tplData.form.form_data;
  var ff_data2 = cloneDeep(ff_data);
  if(!ff_data2.numberFiled && tplData && tplData.form){
      //如果是数值或者复选字段则标记一下，后面计算要用
      var numberFiled = {main:{},children:{}};
      var checkFiled = {main:{},children:{}};
      var __numberShowZeroField__ = {main:[],children:{}};
      if(tplData.form.data_define.main){
            //遍历业务对象
          tplData.form.data_define.main.fields.forEach(function(item){
              if(item.type == 'number'){//如果字段是数值类型
                numberFiled.main[item.code] = true;//追加一个数值标记

                //字段存在为空显示为0的配置
                if(item?.props?.handle_null_value === "show_zero"){
                    __numberShowZeroField__.main.push(item.code);//标记一下配置了显示为0的主表字段code
                }
                
              }
              item.type == 'checkbox_group' && (checkFiled.main[item.code] = true);
          });
      }
      if(tplData.form.data_define.children){
          tplData.form.data_define.children.forEach(function(obj){
              obj.fields.forEach(function(item){
                  if(item.type == 'number') {
                      if (!numberFiled.children[obj.code]){
                          numberFiled.children[obj.code] = {};
                      }
                      numberFiled.children[obj.code][item.code] = true;

                        //子表字段存在为空显示为0的配置
                        if(item?.props?.handle_null_value === "show_zero"){
                            if(!__numberShowZeroField__.children[obj.code]){
                                __numberShowZeroField__.children[obj.code] = [];
                            }
                            __numberShowZeroField__.children[obj.code].push(item.code);
                        }
                  }
                  if(item.type == 'checkbox_group') {
                      if (!checkFiled.children[obj.code]){
                          checkFiled.children[obj.code] = {};
                      }
                      checkFiled.children[obj.code][item.code] = true;
                  }
              });
          });
      }
      ff_data2.numberFiled = numberFiled;
      ff_data2.checkFiled = checkFiled;
      ff_data2.__numberShowZeroField__ = __numberShowZeroField__;
  }
  if(!data_range && !field_sources) return ff_data2;
  var rangeData = merge_range_source(data_range,field_sources);
  var range_main = rangeData.main|| {};
  var range_children = rangeData.children|| {};
  for(var fcode in range_main){
      var range = range_main[fcode];
      if(isArray(range) && range.length>0 && range[0].length > 0){
          if(range[0].length >= 2){
              //这里设计时偶发会把数字变成字符串
              if(isNumber(Number(range[0][0]))){
                  if(hasProperty(ff_data2.main,fcode)){
                      range.forEach(function(r){
                          var v = ff_data2.main[fcode]?ff_data2.main[fcode].toString():"";
                          if(ff_data2.checkFiled && ff_data2.checkFiled.main[fcode]){
                              if([1].toString() === v){//checkbox
                                  ff_data2.main[fcode] = r[1];
                              }
                          }else{
                              if(range[range.length-1].length > 2 && r[2].toString() === v){ //radio,dropdown
                                  ff_data2.main[fcode] = r[1];
                              }
                          }
                      });
                  }
              }
          }
      }
  }
  for(var tcode in range_children){
      var range_stb = range_children[tcode];                
      for(var fcode in range_stb){
          var range = range_stb[fcode];
          if(isArray(range) && range.length>0 && range[0].length > 0){
              if(range[0].length >= 3){
                  if(isNumber(Number(range[0][0]))){
                      if(hasProperty(ff_data2.children,tcode) && ff_data2.children[tcode].length>0){
                          range.forEach(function(r){
                              ff_data2.children[tcode].forEach(function(ff_row){
                                  var v = ff_row[fcode]?ff_row[fcode].toString():"";
                                  if(ff_data2.checkFiled && ff_data2.checkFiled.children[tcode] && ff_data2.checkFiled.children[tcode][fcode]){
                                      if(r[1].toString() === v){
                                          ff_row[fcode] = r[1];
                                      }
                                  }else {
                                      if(range[range.length-1].length > 2 && r[2].toString() === v){ //radio,dropdown
                                          ff_row[fcode] = r[1];
                                      }
                                  }
                              });
                          });
                      }
                  }
              }
          }
      }                
  }

    //遍历业务对象，查找数值类型字段，如果字段没有值，把值设置为0
    //1.处理主表
    isArray(__numberShowZeroField__.main) && __numberShowZeroField__.main.forEach(function(fieldCode){
        if(ff_data2.main.hasOwnProperty(fieldCode)){//主表字段内存在这个数字字段
            let fieldValue = ff_data2.main[fieldCode];//获取字段在主表内的值
            if (fieldValue === null || fieldValue === undefined || fieldValue === "") {//值不存在
                ff_data2.main[fieldCode] = 0;//更新计算数据源这个字段为0
            }
        }
    })
    
    //2.处理子表
    isObject(ff_data2.children) && Object.keys(ff_data2.children).forEach(function(groupCode){
        if(__numberShowZeroField__.children[groupCode]){//当前分组存在为空显示为0的数字字段
            let groupData = ff_data2.children[groupCode];//子表数据
            if(groupData.length > 0){
                groupData.forEach(function(rowData){
                    
                    __numberShowZeroField__.children[groupCode].forEach(function(fieldCode){
                        if(rowData.hasOwnProperty(fieldCode)){//主表字段内存在这个数字字段
                            let fieldValue = rowData[fieldCode];//获取字段在主表内的值
                            if (fieldValue === null || fieldValue === undefined || fieldValue === "") {//值不存在
                                rowData[fieldCode] = 0;//更新计算数据源这个字段为0
                            }
                        }
                    })
                })
            }
        }
    })

  return ff_data2;
}

function merge_range_source(data_range,field_sources){
    var range_main = data_range.main || {};
    var range_children = data_range.children || {};
    var source_main = field_sources.main || {};
    var source_children = field_sources.children || {};
    var data = {main:{},children:{}};
    range_main = cloneDeep(range_main);
    data.main = {...data.main, ...range_main};
    source_main = cloneDeep(source_main);
    data.main = {...data.main, ...source_main}
    range_children = cloneDeep(range_children);
    data.children = {...data.children, ...range_children};
    for(var key in source_children){
        var childrenData = cloneDeep(source_children[key]);
        var data_children = data.children[key]||{};
        data.children[key] = {...data_children,...childrenData};
    }
    return data;
}

function hasProperty(obj,propName){
    if(typeof obj === "object"){
        for(var key in obj){
            if(key === propName) return true;
        }
        return false;
    }else{
        return false;
    }            
}